package com.tz.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParser;
import com.tz.entity.Clazz;
import com.tz.entity.ClazzType;
import com.tz.entity.json.ClazzWithType;
import com.tz.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by oo on 2018/1/3.
 */
@Controller
@RequestMapping("/clazz")
public class ClazzController {

    @Autowired
    ClazzService clazzService;

    @ResponseBody
    @RequestMapping(value = "/getClazzType",produces = "application/json")
    public List<ClazzType> getClazzTyper(){
        List<ClazzType> clazzTypes = clazzService.getClazzType();
        return clazzTypes;
    }

    @ResponseBody
    @RequestMapping(value = "/addClazz",produces = "application/json")
    public String addClazz(@RequestBody String str) throws UnsupportedEncodingException {
        JSONObject json = JSONObject.parseObject(str);
        ClazzType clazzType = new ClazzType();
        Clazz clazz = new Clazz();
        clazzType.setTypeid(json.getLong("clazztypeid"));
        clazz.setClazzname(json.getString("clazzname"));
        clazz.setIpaddress(json.getString("ipaddress"));
        clazz.setRemark(json.getString("remark"));
        clazz.setClazztype(clazzType);
        clazzService.addClazz(clazz);
        return "success!";
    }

    @ResponseBody
    @RequestMapping(value = "/getClazzs",produces = "application/json")
    public List<Clazz> getClazzs(@RequestBody Long typeid){
        ClazzType clazzType = new ClazzType();
        clazzType.setTypeid(typeid);
        List<Clazz> clazz = clazzService.getAllCalzz(clazzType);
        return clazz;
    }
    @ResponseBody
    @RequestMapping(value = "/getClazz",produces = "application/json")
    public Clazz getClazz(@RequestBody Long clazzid){
        Clazz clazz = clazzService.getClazz(clazzid);
        return clazz;
    }

    @ResponseBody
    @RequestMapping(value = "/changeClazz",produces = "application/json")
    public String changeClazz(@RequestBody String str) throws UnsupportedEncodingException {
        System.out.println(str);
        JSONObject json = JSONObject.parseObject(str);
        ClazzType clazzType = new ClazzType();
        Clazz clazz = new Clazz();
        clazzType.setTypeid(json.getLong("clazztypeid"));
        clazz.setClazzid(json.getLong("clazzid"));
        clazz.setIpaddress(json.getString("ipaddress"));
        clazz.setIsfinish(json.getInteger("isfinish"));
        clazz.setRemark(json.getString("remark"));
        clazz.setClazztype(clazzType);
        clazzService.changeClazz(clazz);
        return "success!";
    }
}
